package com.lzp.systemopt.camera.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.animation.LinearInterpolator
import androidx.appcompat.widget.AppCompatImageView
import com.lzp.systemopt.R
import com.lzp.systemopt.camera.Constants

/**
 * @description:
 * @author: lxp
 * @date: 2022/2/9 下午2:47
 */
class CaptureVideoView @kotlin.jvm.JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : AppCompatImageView(context, attrs, defStyle) {

    private var isAnimation = false

    private var swipeAngle = 0f

    private val bgColor = 0xffffffff.toInt()
    private val arcColor = 0xff00ff00.toInt()

    private val centerPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
            color = bgColor
            style = Paint.Style.FILL
        }
    }

    private val arcPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
            color = bgColor
            style = Paint.Style.STROKE
            strokeWidth = 30f
            strokeCap = Paint.Cap.ROUND
        }
    }

    private var margin = 30f

    private val arcRectF by lazy { RectF(margin, margin, width - margin, height - margin) }

    private val centerCircleRadius by lazy { width / 2f - 2 * margin - 30f }
    private var btnState = Constants.STATE_RECORDING
    private val recordTime = 10 * 1000

    private val animation by lazy {
        ValueAnimator.ofInt(0, recordTime).apply {
            duration = recordTime.toLong()
            interpolator = LinearInterpolator()
            addUpdateListener {
                val animatedFraction = it.animatedFraction
                swipeAngle = 360 - animatedFraction * 360
                invalidate()
            }
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
                    onAnimationEnd(animation,false)
                }

                override fun onAnimationCancel(animation: Animator?) {
                    isAnimation = false
                }

                override fun onAnimationEnd(animation: Animator?, isReverse: Boolean) {
                    isAnimation = false
                    timeDownListener?.onTimeDownEnd()
                }
            })
        }
    }

    init {
        startTimeDown()
    }

    fun setState(@Constants.CameraBtnState btnState: Int) {
        if (this.btnState == btnState) return
        this.btnState = btnState
        if (btnState == Constants.STATE_RECORDING) {
            //拍摄中,自定义View
            startTimeDown()
            return
        }
        val stateDrawable = getStateDrawable(btnState)
        setImageResource(stateDrawable)
    }

    private fun startTimeDown() {
        if (isAnimation) {
            stopTimeDown()
        }
        isAnimation = true
        animation.start()
    }

    private fun stopTimeDown() {
        isAnimation = false
        animation.cancel()
    }

    private fun getStateDrawable(@Constants.CameraBtnState btnState: Int): Int {
        return when (btnState) {
            Constants.STATE_DISABLE -> {
                R.mipmap.video_disable
            }
            Constants.STATE_INIT -> {
                R.mipmap.video
            }
            Constants.STATE_PREVIEW -> {
                R.mipmap.video_preview
            }
            else -> {
                R.mipmap.video
            }
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (btnState != Constants.STATE_RECORDING) {
            return
        }
        canvas?.drawCircle(width / 2f, height / 2f, centerCircleRadius, centerPaint)
        //画圆环背景
        arcPaint.color = bgColor
        canvas?.drawArc(arcRectF, 0f, 360f, false, arcPaint)
        //画进度条
        arcPaint.color = arcColor
        canvas?.drawArc(arcRectF, 270f, swipeAngle, false, arcPaint)
    }

    fun getState(): Int {
        return btnState
    }


    var timeDownListener:TimeDownListener? = null

    interface TimeDownListener{
        fun onTimeDownEnd()
    }

}